package com.example.saveqq;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.saveqq.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText1,editText2;
    private Button button,button1;
    private String name,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1=findViewById(R.id.et1);
        editText2=findViewById(R.id.et2);
        button=findViewById(R.id.bt);
        button.setOnClickListener(this);
        button1=findViewById(R.id.bt1);
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.bt) {
            name = editText1.getText().toString();
            password = editText2.getText().toString();
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(password)) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                Log.i("insert", name + password);
                insert(name, password);
            }
        }else{
            Map<String,String> userInfo=find();
            editText1.setText(userInfo.get("key1"));
            editText2.setText(userInfo.get("key2"));
        }

    }
    public void insert(String name,String password){
        MyHelper myHelper=new MyHelper(this);
        SQLiteDatabase db=myHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name1",name);
        values.put("password1",password);
        db.insert("information1",null,values);
        db.close();
    }
    public Map<String,String> find(){
        MyHelper myHelper=new MyHelper(this);
        SQLiteDatabase db=myHelper.getReadableDatabase();
        Cursor cursor=db.query("information1",null,null,null,null,null,null);
        Map<String,String> userInfo=new HashMap<String,String>();
        if(cursor.getCount()!=0){
            cursor.moveToLast();
            userInfo.put("key1",cursor.getString(1));
            userInfo.put("key2",cursor.getString(2));
            db.close();
            cursor.close();
            return userInfo;
        }else{
            Toast.makeText(this, "未查询到数据", Toast.LENGTH_SHORT).show();
            db.close();
            cursor.close();
            return null;
        }
    }

}